package com.zjj.filmcommentapi.service.impl;

import com.auth0.jwt.JWT;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zjj.filmcommentapi.dao.UserMapper;
import com.zjj.filmcommentapi.model.User;
import com.zjj.filmcommentapi.service.UserService;
import com.zjj.filmcommentapi.utils.CodeMsg;
import com.zjj.filmcommentapi.utils.JwtToken;
import com.zjj.filmcommentapi.utils.RedisUtil;
import com.zjj.filmcommentapi.utils.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisUtil redisUtil ;
    @Override
    public Result reg(User user) {
        try {
            if (user.getName()==null||user.getName()==""||user.getAccount()==null||user.getAccount()==""||user.getPassword()==null||user.getPassword()==""){
                return Result.error(CodeMsg.BIND_ERROR);
            }else {
                if (userMapper.selectUserCount(user)!=0){
                    return Result.error(CodeMsg.EXIST_ERROR);
                }else {
                    if (userMapper.insertSelective(user)==1){
                        return Result.success("注册成功",200);
                    }else {
                        return Result.error(CodeMsg.SERVER_ERROR);
                    }
                }
            }
        }catch (Exception E){
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    @Override
    public Result login(User user) {
        if (user.getAccount()==null||user.getAccount()==""||user.getPassword()==null||user.getPassword()==""){
            return Result.error(CodeMsg.BIND_ERROR);
        }
        if(userMapper.selectUserCount(user)==0){
            return  Result.error("此账号不存在请先注册",502);
        }else {
          User myUser = userMapper.selectUserByAccountPass(user);
        if (myUser!=null){
            String account = myUser.getAccount();
            String token = JwtToken.createToken(myUser); //创建token
            redisUtil.renewToken(token,myUser);
            redisUtil.addTokenList(account,token);
            long size = redisUtil.getListSize(account);
            if (size>2){
                redisUtil.listTrim(account,size-1,size-2);
            }
            return Result.success(token,200);
        }else {
            return Result.error("登陆失败，账号或密码错误",502);
        }
        }
    }

    @Override
    public Result getUserList() {
        return Result.success(userMapper.selectUserList());
    }

    @Override
    public Result updateUser(User user) {
        if (userMapper.updateUserStateById(user)==1){
            return Result.success("更新成功",200);
        }else {
            return Result.error("更新失败",502);
        }
    }
    @Override
    public User findUserById(User user){
        return  userMapper.findUserById(user);
    }

    @Override
    public Boolean logout(String token) {
        try {
            String account = JWT.decode(token).getAudience().get(0);
            redisUtil.removeListItem(account,0,token);
            redisUtil.del(token);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User getUserInfo(String token) {
        try {
            String account = JWT.decode(token).getAudience().get(0);
            User user = (User) redisUtil.sentinelGet(token);
            if (user!=null){
                user.setPassword("");
            }
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
